package copy.express.pojo;


import copy.express.process.spit.ConfirmTypeEndIndex;
import copy.express.process.spit.impl.*;
import copy.express.process.spit.impl.buffer.FieldMethodTypeIndex;
import copy.express.process.spit.impl.buffer.StringMethodTypeIndex;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class NodeStack extends Stack {
    private String solveStr;
    private boolean containMethod = false;
    List<ConfirmTypeEndIndex> confirmTypeEndIndeList  = new ArrayList<ConfirmTypeEndIndex>();
    List<ConfirmTypeEndIndex> confirmMethodTypeEndIndeList  = new ArrayList<ConfirmTypeEndIndex>();
    //构造方法
    public NodeStack(String solveStr,int model) {
        this.solveStr = solveStr;
        doSolve(model);

    }

    //解析器: 解析IF的运算
    {

        confirmMethodTypeEndIndeList.add( StringMethodTypeIndex.getInstance() );
        confirmMethodTypeEndIndeList.add( SymbolTypeEndIndex.getInstance() );
        confirmMethodTypeEndIndeList.add( NumberTypeEndInex.getInstance() );
        confirmMethodTypeEndIndeList.add(FieldMethodTypeIndex.getInstance() );

    }
    //解析器: 解析普通运算
    {

        confirmTypeEndIndeList.add( StringTypeEndIndex.getInstance() );
        confirmTypeEndIndeList.add( SymbolTypeEndIndex.getInstance() );
        confirmTypeEndIndeList.add( NumberTypeEndInex.getInstance() );
        confirmTypeEndIndeList.add( FieldTypeEndIndex.getInstance() );

    }



    private void doSolve(int model){
        List<ConfirmTypeEndIndex> processList = null;
        if (model == 1){
            processList = confirmTypeEndIndeList;
        }else if (model == 2){
            processList = confirmMethodTypeEndIndeList;
        }
        char[] chars = solveStr.toCharArray();
        //判断顺序1.字符串 2.符号 3.字段
        for (int i = 0; i < chars.length;  ){
            if ( chars[i] == ' ' || chars[i] == ',' ){
                i++;
                continue;
            }
            int endIndex = -1;
            for (ConfirmTypeEndIndex confirmTypeIndex : processList) {
                if ( confirmTypeIndex.confirmType(chars[i]) ){
                    if (i+1 == chars.length){
                        endIndex = i+1;
                    }else{
                        //开始寻找结束位置
                        endIndex = confirmTypeIndex.findEndIndex(i + 1, i + 1, chars);
                    }
                    //根据开始位置和结束位置: 获取节点Node
                    NodeType node = confirmTypeIndex.createNode(i, endIndex, chars);
                    //将节点保存到容器中
                    add(node);
                    //更新坐标

                    i = endIndex;
                    break;
                }
            }
            if (i>=chars.length){
                break;
            }
        }

    }


    public Stack<Node> getQueue() {
        return this;
    }
}
